import setting from '@/setting.js'
// 价格过滤
function handleFilterPrice(price = null) {
	if (!/^[0-9]+$/.test(price)) {
		console.log('"' + price + '"不是正整数');
		return price;
	}

	let new_price = 0;

	new_price = Number(price) / 100;

	return new_price;
};

// 价格还原
function handleRevertPrice(price = null) {
	let new_price = 0;

	new_price = Number(price) * 100;

	return new_price.toFixed(0);
};

// 数量过滤，超过万显示w，超过千显示k
function handleFilterQuantity(quantity = null) {
	if (!/^[0-9]+$/.test(quantity)) {
		console.log('"' + quantity + '"不是正整数');
		return quantity;
	}

	let new_quantity = "";

	if (quantity >= 10000) {
		new_quantity = (quantity / 10000).toFixed(1) + 'w';
	} else if (quantity >= 1000) {
		new_quantity = (quantity / 1000).toFixed(1) + 'k';
	} else {
		new_quantity = quantity;
	}

	return new_quantity;
};

// 价格过滤并且数量过滤
function handleFilterPriceQuantity(price = null) {
	let new_price = "";

	price = handleFilterPrice(price);

	new_price = handleFilterQuantity(price);

	return new_price;
};

// 折扣过滤
function handleFilterDiscount(discount = null) {
	if (!/^[0-9]+$/.test(discount)) {
		console.log('"' + discount + '"不是正整数');

		return discount;
	}

	let new_discount = 0;

	new_discount = Number(discount) / 10;

	return new_discount;
};

// 收费模式
function handleFeeType(value) {
	let feeTypeList = [{
			value: "1",
			label: '支付费用-手动输入'
		},
		{
			value: '2',
			label: '支付费用-费用协商'
		},
		{
			value: '3',
			label: '希望互免'
		},
	];
	let text = "";

	feeTypeList.forEach((item, index) => {
		if (item.value == value) {
			text = item.label
		}
	})
	
	if(value == 1) {
		return "";
	}
	
	if(value == 2) {
		return "费用协商";
	}

	return text;
};

// 通告费用
function handleChargingAmount(value) {
	let data = JSON.parse(value);
	let text = "";

	if (data.is_payment_range == false) {
		text = data.min_fee
	}
	if (data.is_payment_range == true) {
		text = data.min_fee + '-' + data.max_fee
	}
	
	text += data.unit;
	if(text == 'undefined' || text == undefined || text == null || text == 0) {
		return "";
	}
	
	return "￥" + text;
};

// 性别
function handleSex(value) {
	let list = [{
			value: "0",
			label: '不限'
		},
		{
			value: '1',
			label: '男'
		},
		{
			value: '2',
			label: '女'
		},
	];
	let text = "";

	list.forEach((item, index) => {
		if (item.value == value) {
			text = item.label
		}
	})

	return text;
};

// 任务奖励
function handleTaskRewardMethod(value) {
	let list = [{
			value: "1",
			label: '有稿费'
		},
		{
			value: '2',
			label: '赠送产品'
		},
		{
			value: '3',
			label: '稿费+赠送产品'
		},
	];
	let text = "";

	list.forEach((item, index) => {
		if (item.value == value) {
			text = item.label
		}
	})

	return text;
};

// 粉丝要求
function handleLimitFans(value) {
	let data = JSON.parse(value);
	let text = "";
	if (data.is_fans_range == true) {
		text = "不限制"
	}
	if (data.is_fans_range == false) {
		text = data.min_fans + '-' + data.max_fans
	}

	return text;
};

// 平台
function handleMarketingPlatform(value) {
	let list = getApp().globalData.platformList;
	
	let text = "";

	list.forEach((item,index) => {
		if(item.value == value) {
			text = item.label
		}
	})
	return text;
};


// 平台logo
function handleMarketingPlatformLogo(value) {
	let list = getApp().globalData.platformList;
	
	let logo = "";

	list.forEach((item,index) => {
		if(item.value == value) {
			logo = item.extra
		}
	})
	return logo;
};

// 身份处理
function handleUserIdentity(userInfo) {
	let text = "";

	if (!uni.$u.test.isEmpty(userInfo.identity_master)) {
		text += userInfo.identity_master.name;
	}

	if (!uni.$u.test.isEmpty(userInfo.identity1)) {
		text += " · " + userInfo.identity1.name;
	}

	if (!uni.$u.test.isEmpty(userInfo.identity2)) {
		text += " · " + userInfo.identity2.name;
	}

	return text;
};

const $filter = {
	price: handleFilterPrice,
	revertPrice: handleRevertPrice,
	quantity: handleFilterQuantity,
	priceQuantity: handleFilterPriceQuantity,
	discount: handleFilterDiscount,
	handleFeeType: handleFeeType,
	handleChargingAmount: handleChargingAmount,
	handleSex: handleSex,
	handleTaskRewardMethod: handleTaskRewardMethod,
	handleLimitFans: handleLimitFans,
	handleMarketingPlatform: handleMarketingPlatform,
	handleMarketingPlatformLogo: handleMarketingPlatformLogo,
	handleUserIdentity: handleUserIdentity,
}


const install = Vue => {
	Vue.filter('filterPrice', (data) => {
		return handleFilterPrice(data)
	})
	Vue.filter('filterQuantity', (data) => {
		return handleFilterQuantity(data)
	})
	Vue.filter('filterPriceQuantity', (data) => {
		return handleFilterPriceQuantity(data)
	})
	Vue.filter('filterDiscount', (data) => {
		return handleFilterDiscount(data)
	})
	Vue.filter('filterFeeType', (data) => {
		return handleFeeType(data)
	})
	Vue.filter('filterChargingAmount', (data) => {
		return handleChargingAmount(data)
	})
	Vue.filter('filterMarketingPlatform', (data) => {
		return handleMarketingPlatform(data)
	})
	Vue.filter('filterMarketingPlatformLogo', (data) => {
		return handleMarketingPlatformLogo(data)
	})
	Vue.filter('filterTaskRewardMethod', (data) => {
		return handleTaskRewardMethod(data)
	})
	Vue.filter('filterIdentity', (data) => {
		return handleUserIdentity(data)
	})
	Vue.filter('filterLimitFans', (data) => {
		return handleLimitFans(data)
	})

	Vue.prototype.$filter = $filter
}

export default {
	install
}
